package com.ruoyi.gczl.sybg.ws.tt.controller;

import com.ruoyi.common.GetFileName;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gczl.sybg.ws.tt.domain.SybgTtws;
import com.ruoyi.gczl.sybg.ws.tt.service.ISybgTtwsService;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 试验报告天台无损Controller
 *
 * @author huacai
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/sybg/ttws")
public class SybgTtwsController extends BaseController {
    @Resource
    private ISybgTtwsService sybgTtwsService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询试验报告天台无损列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:list')")
    @GetMapping("/list")
    public TableDataInfo list(SybgTtws sybgTtws) {
        startPage();
        List<SybgTtws> list = sybgTtwsService.selectSybgTtwsList(sybgTtws);
        return getDataTable(list);
    }

    /**
     * 导出试验报告天台无损列表
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:export')")
    @Log(title = "试验报告天台无损", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SybgTtws sybgTtws) {
        List<SybgTtws> list = sybgTtwsService.selectSybgTtwsList(sybgTtws);
        ExcelUtil<SybgTtws> util = new ExcelUtil<SybgTtws>(SybgTtws.class);
        util.exportExcel(response, list, "试验报告天台无损数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SybgTtws> util = new ExcelUtil<SybgTtws>(SybgTtws.class);
        util.importTemplateExcel(response, "试验报告天台无损数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "试验报告天台无损", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sybg:ttws:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<SybgTtws> util = new ExcelUtil<SybgTtws>(SybgTtws.class);
        InputStream inputStream = file.getInputStream();
        List<SybgTtws> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sybgTtwsService.batchInsertSybgTtws(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取试验报告天台无损详细信息
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:query')")
    @GetMapping(value = "/{sybgTtwsId}")
    public AjaxResult getInfo(@PathVariable("sybgTtwsId") Integer sybgTtwsId) {
        return success(sybgTtwsService.selectSybgTtwsBySybgTtwsId(sybgTtwsId));
    }

    /**
     * 新增试验报告天台无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:add')")
    @Log(title = "试验报告天台无损", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SybgTtws sybgTtws) {
        sybgTtws.setUserId(Math.toIntExact(getUserId()));
        sybgTtws.setDeptId(Math.toIntExact(getDeptId()));
        sybgTtws.setCreateBy(getUsername());
        int i = sybgTtwsService.insertSybgTtws(sybgTtws);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(String.valueOf(sybgTtws.getSybgTtwsId()));
        infoRunTaskData.setBusinessType(12); //业务模块: 试验报告-无损-天台
        String fileNameOnly = GetFileName.getFileNameOnly(sybgTtws.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 文件名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改试验报告天台无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:edit')")
    @Log(title = "试验报告天台无损", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SybgTtws sybgTtws) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        String fileNameOnly = GetFileName.getFileNameOnly(sybgTtws.getFileName());
        infoRunTaskData.setBusinessTitle(fileNameOnly); //任务名称: 文件名称
        infoRunTaskData.setBusinessKey(String.valueOf(sybgTtws.getSybgTtwsId()));
        infoRunTaskData.setBusinessType(12); //业务模块: 试验报告-无损-天台
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(sybgTtwsService.updateSybgTtws(sybgTtws));
    }

    /**
     * 删除试验报告天台无损
     */
    @PreAuthorize("@ss.hasPermi('sybg:ttws:remove')")
    @Log(title = "试验报告天台无损", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sybgTtwsIds}")
    public AjaxResult remove(@PathVariable Integer[] sybgTtwsIds) {
        //同时删除业务关系数据
        for (Integer sybgTtwsId : sybgTtwsIds) {
            SybgTtws sybgTtws = sybgTtwsService.selectSybgTtwsBySybgTtwsId(sybgTtwsId);
            iInfoRunTaskDataService.deleteData(String.valueOf(sybgTtws.getSybgTtwsId()), 12);//业务模块: 试验报告-无损-天台
        }
        return toAjax(sybgTtwsService.deleteSybgTtwsBySybgTtwsIds(sybgTtwsIds));
    }
}
